package dao;

import java.sql.*;
import java.util.*;
import to.ClienteTO;

public class ClienteDAO extends GenericDAO{
    
    public ClienteDAO(Transaction transacction) {
	super(transacction);
    }

    @SuppressWarnings("unchecked")
    public List<ClienteTO> buscarCliente(ClienteTO to) throws TransactionException {
        Connection conn;
        List<ClienteTO> list = new ArrayList();
        try {
            int i = 1;
            conn = (Connection) t.getConnection();
            String sqlCall = "{call uspBuscarCliente(?,?,?,?,?,?)}";
            CallableStatement procedure = conn.prepareCall(sqlCall);
            procedure.setString(i++, to.getCCliente());
            procedure.setString(i++, to.getDApellidoPaterno());
            procedure.setString(i++, to.getDApellidoMaterno());
            procedure.setString(i++, to.getDNombres());
            procedure.setString(i++, to.getCTipoDocumento());
            procedure.setString(i++, to.getNDocumento());
            ResultSet rs = procedure.executeQuery();

            while (rs.next()) {
                int j=0;
                ClienteTO vo = new ClienteTO(); 
                vo.setCCliente(rs.getString(++j));
                vo.setDApellidoPaterno(rs.getString(++j));
                vo.setDApellidoMaterno(rs.getString(++j));
                vo.setDNombres(rs.getString(++j));
                vo.setPDescuento(rs.getDouble(++j));
                list.add(vo);
            }

            procedure.close();
            conn.close();

        } catch (SQLException e1) {
                throw new TransactionException(e1);
        }
        return list;
    }
}
